/**
 * @file thread_idle.c
 * @brief 
 * 
 * @author dalin (dalin@open-robot.com)
 * @version 1.0
 * @date 2023-05-09
 * 
 * @copyright Copyright (c) 2023  Open Robot Tech.co, Ltd
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version <th>Author  <th>Description
 * <tr><td>2023-05-09 <td>1.0     <td>dalin     <td>内容
 * </table>
 */

#include "FreeRTOS.h"
#include "cmsis_os.h"

#include "mlog.h"
#include "user_data.h"
#include "thread_idle.h"
#include "thread_config.h"


static void thread_stacks_infomation(void)
{
#if 0
    UBaseType_t ux[THREAD_ID_MAX];

    for(int i = 0; i < THREAD_ID_MAX; i++)
    {
        ux[i] = uxTaskGetStackHighWaterMark(TaskHandle[i]);
    }
    
    for(int i = 0; i < THREAD_ID_MAX; i++)
    {
        log_info("task%d stacks:%d", i, ux[i]);
    }
#else /* 需要在Include definetions部分使能eTaskGetState */
	uint8_t mytaskstatebuffer[500];
	printf("---------------------- TASKS STACK----------------------\r\n");
	printf("TaskName     	TaskSte 	Pri   	FreeStack TaskID\r\n");
	osThreadList((uint8_t *)&mytaskstatebuffer);
	printf("%s\r\n",mytaskstatebuffer);
	printf("--------------------------------------------------------\r\n");
	

#endif
}


void idle_thread(void const * argument)
{
    // printf("user wheel start\r\n");
    osDelay(INTO_THREAD_DELAY);
	log_base("into idle task");
	
	thread_stacks_infomation();
    for(;;)
    {
		
		if(udata.sys.task_cnt.idle_cnt % 5 == 0)
			log_info("[TASK COUNT]control:%-5d commun:%-5d exception:%-5d dispaly:%-5d idle:%-5d ",
                    udata.sys.task_cnt.control_cnt,
                    udata.sys.task_cnt.commun_cnt,
                    udata.sys.task_cnt.exception_cnt,
                    udata.sys.task_cnt.display_cnt,
                    udata.sys.task_cnt.idle_cnt
                    );

//        if(udata.sys.task_cnt.idle_cnt == 0)
            

        osDelay(THREAD_DELAY_IDLE);

         /* 任务计数 */
         udata.sys.task_cnt.idle_cnt++;
    }
}

